package com.dao.impl;

import com.dao.UserDao;
import com.pojo.User;
import com.util.BaseJdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {


    @Override
    public User getuserOne(User user) {
        User user1 = new User();
       try {
           String sql="select * from ly_user where name=? and password=?";
           Object[] objects = new Object[2];
           objects[0]=user.getUserName();
           objects[1]=user.getPassword();
           ResultSet resultSet = BaseJdbc.BaseSelect(sql, objects);
           while (resultSet.next()){
                user1.setId(resultSet.getInt("id"));
                user1.setUserName(resultSet.getString("name"));
                user1.setPassword(resultSet.getString("password"));
           }
       }catch (Exception e){
           e.printStackTrace();
       }
        return user1;
    }

    @Override
    public List<User> getuserAll(int page,int limit) {
        List<User> list = new ArrayList<User>();
        try {
            String sql="select * from ly_user limit ?,?";
            Object[] objects = new Object[2];
            objects[0]=page;
            objects[1]=limit;
            ResultSet resultSet = BaseJdbc.BaseSelect(sql, objects);
            while (resultSet.next()){
                User user1 = new User();
                user1.setId(resultSet.getInt("id"));
                user1.setUserName(resultSet.getString("name"));
                user1.setPassword(resultSet.getString("password"));
                user1.setAccountName(resultSet.getString("accountName"));
                user1.setSex(resultSet.getInt("sex"));
                list.add(user1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
    @Override
    public List<User> getuserAll() {
        List<User> list = new ArrayList<User>();
        try {
            String sql="select * from ly_user";
            Object[] objects = new Object[1];
            ResultSet resultSet = BaseJdbc.BaseSelect(sql, objects);
            while (resultSet.next()){
                User user1 = new User();
                user1.setId(resultSet.getInt("id"));
                user1.setUserName(resultSet.getString("name"));
                user1.setPassword(resultSet.getString("password"));
                user1.setAccountName(resultSet.getString("accountName"));
                user1.setSex(resultSet.getInt("sex"));
                list.add(user1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }


    @Override
    public int delUser(int id) throws SQLException {
        String sql="delete from ly_user where id=? ";
        Object[] objects = new Object[1];
        objects[0]=id;
        int i = BaseJdbc.BaseUpdate(sql, objects);
        return i;
    }

    @Override
    public int upateUser(User user) throws SQLException {
        String sql="update  ly_user set name=? ,password=? where id=? ";
        Object[] objects = new Object[3];
        objects[0]=user.getUserName();
        objects[1]=user.getPassword();
        objects[2]=user.getId();
        int i = BaseJdbc.BaseUpdate(sql, objects);
        return i;
    }

    @Override
    public int insertUser(User user) throws SQLException {
        String sql="insert into  ly_user(name,password,picPath)value(?,?,?) ";
        Object[] objects = new Object[3];
        objects[0]=user.getUserName();
        objects[1]=user.getPassword();
        objects[2]=user.getPicPath();
        int i = BaseJdbc.BaseUpdate(sql, objects);
        return i;
    }

    @Override
    public User getuserOne(int id) {
        User user1 = new User();
        try {
            String sql="select * from user_table61 where id=?";
            Object[] objects = new Object[1];
            objects[0]=id;
            ResultSet resultSet = BaseJdbc.BaseSelect(sql, objects);
            while (resultSet.next()){
                user1.setId(resultSet.getInt("id"));
                user1.setUserName(resultSet.getString("uname"));
                user1.setPassword(resultSet.getString("password"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return user1;
    }
}
